import { toFixedCustomize } from '@/utils/bizUtils'
/**
 * request logic
 */
const NET_CODE_SUC = 200;

/**
 * 判断服务请求是否成功
 * @param response
 * @returns {*|boolean}
 */
export function isResponseSuccess (response) {
  return response && response.code && response.code === NET_CODE_SUC;
}

/**
 * 供应商 supplier
*/
// 供应商状态 合格、潜在
export const SUPPLIER_QUALIFIED_STATUS_CODE = 40332;
export const SUPPLIER_QUALIFIED_STATUS_DES = '合格';
export const SUPPLIER_POTENTIAL_STATUS_CODE = 40331;
export const SUPPLIER_POTENTIAL_STATUS_DES = '潜在';
export const SUPPLIER_STATUS_LIST = [
  { id: SUPPLIER_QUALIFIED_STATUS_CODE, name: SUPPLIER_QUALIFIED_STATUS_DES },
  { id: SUPPLIER_POTENTIAL_STATUS_CODE, name: SUPPLIER_POTENTIAL_STATUS_DES }
]

/**
 * 基本信息
 */
// 询价类型 Inquiry 客户询价、订货询价
export const INQUIRY_TYPE_CUSTOMER_CODE = 10;
export const INQUIRY_TYPE_CUSTOMER_DES = '客户询价';
export const INQUIRY_TYPE_ORDER_CODE = 11;
export const INQUIRY_TYPE_ORDER_DES = '订货询价';

/**
 * 商品信息
 */
// 商品类型 0正常商品 1临采商品 2客户专属商品
export const PRODUCT_TYPE_NORMAL_CODE = '0';
export const PRODUCT_TYPE_NORMAL_DES = '正常商品';
export const PRODUCT_TYPE_TEMPORARY_CODE = '1';
export const PRODUCT_TYPE_TEMPORARY_DES = '临采商品';
export const PRODUCT_TYPE_EXCLUSIVE_CODE = '2';
export const PRODUCT_TYPE_EXCLUSIVE_DES = '客户专属商品';

/**
 * 报价信息
 * {
      0: '未报价',
      1:'全部报价',
      2:'报价中',
      3:'部份报价',
      4:'供应商退回',
      5:'已过期',
    }
 */
// 供应商报价状态
export const QUOTE_DONE_STATUS_DES = '已报价';
export const QUOTE_DONE_STATUS_CODE = 1;
export const QUOTE_UNDONE_STATUS_DES = '未报价';
export const QUOTE_UNDONE_STATUS_CODE = 0;
// 报价中
export const QUOTE_PENDING_STATUS_CODE = 2;
// 部分报价
export const QUOTE_PART_STATUS_CODE = 3;

// 运费承担方 Freight bearer
export const UNDERTAKER_SELF_DES = '我司承担';
export const UNDERTAKER_SELF_CODE = 40357;
export const UNDERTAKER_SUPPLIER_DES = '供应商承担';
export const UNDERTAKER_SUPPLIER_CODE = 40358;
export const UNDERTAKER_NONE_DES = '无运费';
export const UNDERTAKER_NONE_CODE = 40359;

export const UNDERTAKER_ARR = [
  { id: UNDERTAKER_SELF_CODE, name: UNDERTAKER_SELF_DES },
  { id: UNDERTAKER_SUPPLIER_CODE, name: UNDERTAKER_SUPPLIER_DES },
  { id: UNDERTAKER_NONE_CODE, name: UNDERTAKER_NONE_DES },
]

// 发票类型
export const INVOICE_TYPE_COMMON_DES = '普通发票';
export const INVOICE_TYPE_COMMON_CODE = 101;
export const INVOICE_TYPE_SPECIAL_DES = '增值税专用发票';
export const INVOICE_TYPE_SPECIAL_CODE = 102;
export const INVOICE_TYPE_NONE_DES = '无税票';
export const INVOICE_TYPE_NONE_CODE = 103;

export const INVOICE_TYPE_ARR = [
  { id: INVOICE_TYPE_COMMON_CODE, name: INVOICE_TYPE_COMMON_DES },
  { id: INVOICE_TYPE_SPECIAL_CODE, name: INVOICE_TYPE_SPECIAL_DES },
  { id: INVOICE_TYPE_NONE_CODE, name: INVOICE_TYPE_NONE_DES },
]

// 税率
export const TAXRATE_39874_DES = '0';
export const TAXRATE_39874_CODE = 39874;
export const TAXRATE_39875_DES = 0.03;
export const TAXRATE_39875_CODE = 39875;
export const TAXRATE_39876_DES = 0.05;
export const TAXRATE_39876_CODE = 39876;
export const TAXRATE_39877_DES = 0.06;
export const TAXRATE_39877_CODE = 39877;
export const TAXRATE_39878_DES = 0.09;
export const TAXRATE_39878_CODE = 39878;
export const TAXRATE_39879_DES = 0.13;
export const TAXRATE_39879_CODE = 39879;
export const TAXRATE_40003_DES = 0.01;
export const TAXRATE_40003_CODE = 40003;
export const TAXRATE_40121_DES = 0.16;
export const TAXRATE_40121_CODE = 40121;

export const TAXRATE_ARR = [
  { id: TAXRATE_39875_CODE, name: TAXRATE_39875_DES },
  { id: TAXRATE_39876_CODE, name: TAXRATE_39876_DES },
  { id: TAXRATE_39877_CODE, name: TAXRATE_39877_DES },
  { id: TAXRATE_39878_CODE, name: TAXRATE_39878_DES },
  { id: TAXRATE_39879_CODE, name: TAXRATE_39879_DES },
  { id: TAXRATE_40003_CODE, name: TAXRATE_40003_DES },
  { id: TAXRATE_40121_CODE, name: TAXRATE_40121_DES },
]

export const TAXRATE_0_ARR = [
  { id: TAXRATE_39874_CODE, name: TAXRATE_39874_DES }
]

// 报价明细 - 询价表格信息
export const QUOTE_COLUMNS_INQUERY = [
  {
    prop: 'skuStatusName',
    label: '商品状态',
    fixed: 'left',
  },
  {
    prop: 'factoryCode',
    label: '厂家商品代码',
    fixed: 'left',
    width: '100'
  },
  {
    prop: 'factoryName',
    label: '厂家商品名称',
    fixed: 'left',
    width: '100'
  },
  {
    prop: 'skuTypeStr',
    label: '商品类型',
    fixed: 'left',
  },
  {
    prop: 'skuCode',
    label: '商品代码',
    fixed: 'left',
  },
  {
    prop: 'skuName',
    label: '商品名称',
    fixed: 'left',
  },
  {
    prop: 'skuSpecifications',
    label: '规格型号',
    fixed: 'left',
  },
  {
    prop: 'skuUnit',
    label: '主计量单位',
  },
  {
    prop: 'assistSkuUnit',
    label: '辅计量单位',
  },
  {
    prop: 'addPriceTypeStr',
    label: '加价类型',
  },
  {
    prop: 'rebateRatio',
    label: '返利折合比例/金额',
  },
  {
    prop: 'skuNum',
    label: '主数量',
  },
  {
    prop: 'assistSkuNum',
    label: '辅数量',
    width: '150'
  },
  {
    prop: 'unitPrice',
    label: '主进价',
    width: '120'
  },
  {
    prop: 'supplierPrice',
    label: '供应商报价',
    width: '120'
  },
  {
    prop: 'assistUnitPrice',
    label: '辅进价',
    width: '150'
  },
  // 询价线下采购报价时展示
  {
    prop: 'deliveryDate',
    label: '交货期',
  },
  {
    prop: 'priceExpireTime',
    label: '价格有效期',
    width: 245
  },
  {
    prop: 'remark',
    label: '采购备注',
    width: '150'
  },
  {
    prop: 'returnReason',
    label: '退回原因',
  },
  {
    prop: 'returnRemark',
    label: '退回备注',
  },
  {
    prop: 'sharePrice',
    label: '费用分摊金额',
  },
  {
    prop: 'costPrice',
    label: '成本价',
  },
  {
    prop: 'totalPrice',
    label: '小计',
  },
  {
    prop: 'purchaseSkuName',
    label: '采购商品名称',
  },
  {
    prop: 'skuBrand',
    label: '品牌',
  },
  {
    prop: 'salesmanRemark',
    label: '销售备注',
  },
  {
    prop: 'qualification',
    label: '资质要求',
  },
  // 询价需求才会展示
  {
    prop: 'requiredInquiryTime',
    label: '要求采购询价时间',
  },
  {
    prop: 'scmRemark',
    label: '供应商备注',
    width: 100
  },
  {
    prop: 'selectAllQualification',
    label: '资质全选',
  },
  {
    prop: 'certificate',
    label: '合格证',
  },
  {
    prop: 'threeC',
    label: '3C',
  },
  {
    prop: 'testReport',
    label: '检测报告',
  },
  {
    prop: 'materialList',
    label: '材质单',
  },
];
// 报价明细 - 订货表格信息
export const QUOTE_COLUMNS_ORDER = [
  {
    prop: 'skuStatusName',
    label: '商品状态',
    fixed: 'left',
  },
  {
    prop: 'factoryCode',
    label: '厂家商品代码',
    fixed: 'left',
    width: '100'
  },
  {
    prop: 'factoryName',
    label: '厂家商品名称',
    fixed: 'left',
    width: '100'
  },
  {
    prop: 'skuTypeStr',
    label: '商品类型',
    fixed: 'left',
  },
  {
    prop: 'skuCode',
    label: '商品代码',
    fixed: 'left',
  },
  {
    prop: 'skuName',
    label: '商品名称',
    fixed: 'left',
  },
  {
    prop: 'skuSpecifications',
    label: '规格型号',
    fixed: 'left',
  },
  {
    prop: 'skuUnit',
    label: '主计量单位',
  },
  {
    prop: 'assistSkuUnit',
    label: '辅计量单位',
  },
  {
    prop: 'addPriceTypeStr',
    label: '加价类型',
  },
  {
    prop: 'rebateRatio',
    label: '返利折合比例/金额',
  },
  {
    prop: 'skuNum',
    label: '主数量',
  },
  {
    prop: 'assistSkuNum',
    label: '辅数量',
    width: '100'
  },
  {
    prop: 'unitPrice',
    label: '主进价',
    width: '100'
  },
  {
    prop: 'supplierPrice',
    label: '供应商报价',
    width: '120'
  },
  {
    prop: 'assistUnitPrice',
    label: '辅进价',
    width: '100'
  },
  {
    prop: 'workableArrivalTime',
    label: '落实到货时间',
    width: '245'
  },
  {
    prop: 'priceExpireTime',
    label: '价格有效期',
    width: '245'
  },
  {
    prop: 'scmRemark',
    label: '供应商备注',
    width: 100
  },
  {
    prop: 'remark',
    label: '采购备注',
    width: '150'
  },
  {
    prop: 'returnReason',
    label: '退回原因',
  },
  {
    prop: 'returnRemark',
    label: '退回备注',
  },
  {
    prop: 'sharePrice',
    label: '费用分摊金额',
  },
  {
    prop: 'costPrice',
    label: '成本价',
  },
  {
    prop: 'totalPrice',
    label: '小计',
  },
  {
    prop: 'purchaseSkuName',
    label: '采购商品名称',
  },
  {
    prop: 'skuBrand',
    label: '品牌',
  },
  {
    prop: 'salesmanRemark',
    label: '销售备注',
  },
  {
    prop: 'qualification',
    label: '资质要求',
  },
  {
    prop: 'requiredArrivalTime',
    label: '要求到货时间',
  },
  {
    prop: 'selectAllQualification',
    label: '资质全选',
  },
  {
    prop: 'certificate',
    label: '合格证',
  },
  {
    prop: 'threeC',
    label: '3C',
  },
  {
    prop: 'testReport',
    label: '检测报告',
  },
  {
    prop: 'materialList',
    label: '材质单',
  },
];

export function toFixed (num, s) {
  let times = Math.pow(10, s);
  let des = num * times + 0.5;
  des = parseInt(des, 10) / times;
  return des + '';
}
// 定标页查看 合计
export const summaryMethod = ({ columns, data }) => {
  const means = [] // 合计
  const summaryList = ['skuPrice', 'totalPrice', 'costPrice']; // 需要合计的字段合集
  columns.forEach((column, columnIndex) => {
    if (columnIndex === 0) {
      means.push('合计')
    } else if (column.property && summaryList.includes(column.property.split('_')[0])) {
      const values = data.map(item => Number(item[column.property]));
      // 合计
      if (!values.every(value => isNaN(value))) {
        means[columnIndex] = values.reduce((prev, curr) => {
          const value = Number(curr);
          if (!isNaN(value)) {
            return prev + curr;
          } else {
            return prev;
          }
        }, 0);
        means[columnIndex] = '<span style="font-weight: bold;">' + toFixedCustomize(means[columnIndex]) + '元</span>'
      } else {
        means[columnIndex] = '';
      }
    }
  })
  // 返回一个二维数组的表尾合计
  return [means]
}

export const summaryMethodCommon = ({ columns, data }, summaryList) => {
  const means = [] // 合计
  columns.forEach((column, columnIndex) => {
    if (columnIndex === 0) {
      means.push('合计')
    } else if (column.property && summaryList.includes(column.property.split('_')[0])) {
      const values = data.map(item => Number(item[column.property]));
      // 合计
      if (!values.every(value => isNaN(value))) {
        means[columnIndex] = values.reduce((prev, curr) => {
          const value = Number(curr);
          if (!isNaN(value)) {
            return prev + curr;
          } else {
            return prev;
          }
        }, 0);
        means[columnIndex] = '<span style="font-weight: bold;">' + toFixedCustomize(means[columnIndex]) + '元</span>'
      } else {
        means[columnIndex] = '';
      }
    }
  })
  // 返回一个二维数组的表尾合计
  return [means]
}
